package io.github.wanggit.antrpc.console.web.vo;

import io.github.wanggit.antrpc.commons.bean.SubscribeNode;
import io.github.wanggit.antrpc.commons.utils.JsonUtils;
import io.github.wanggit.antrpc.console.web.utils.LongToDateUtil;

import java.io.Serializable;
import java.util.Objects;

public class SubscribeNodeVO extends SubscribeNode implements Serializable {
    private static final long serialVersionUID = 6998859209070585823L;

    public String getTsStr() {
        return LongToDateUtil.toDate(super.getTs());
    }

    public SubscribeNode toSubscribeNode() {
        SubscribeNode subscribeNode = new SubscribeNode();
        subscribeNode.setClassName(getClassName());
        subscribeNode.setHost(getHost());
        subscribeNode.setTs(getTs());
        subscribeNode.setMethodMap(getMethodMap());
        return subscribeNode;
    }

    public static SubscribeNodeVO fromSubscribeNode(SubscribeNode subscribeNode) {
        SubscribeNodeVO subscribeNodeVO = new SubscribeNodeVO();
        subscribeNodeVO.setHost(subscribeNode.getHost());
        subscribeNodeVO.setClassName(subscribeNode.getClassName());
        subscribeNodeVO.setMethodMap(subscribeNode.getMethodMap());
        subscribeNodeVO.setTs(subscribeNode.getTs());
        return subscribeNodeVO;
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof SubscribeNodeVO)) {
            return false;
        }
        SubscribeNodeVO other = (SubscribeNodeVO) o;
        return Objects.equals(getHost(), other.getHost())
                && Objects.equals(getClassName(), other.getClassName())
                && Objects.equals(getTsStr(), other.getTsStr())
                && Objects.equals(
                        JsonUtils.toJsonString(getMethodMap()),
                        JsonUtils.toJsonString(other.getMethodMap()));
    }

    @Override
    public int hashCode() {
        return getHost().hashCode()
                & getClassName().hashCode()
                & getTsStr().hashCode()
                & JsonUtils.toJsonString(getMethodMap()).hashCode();
    }

    @Override
    public String toString() {
        return "host="
                + getHost()
                + ", className="
                + getClassName()
                + ", ts="
                + getTsStr()
                + ", methodMap="
                + JsonUtils.toJsonString(getMethodMap())
                + ", super.toString()="
                + super.toString();
    }
}
